/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.dwusoft.buss.common  
 */
package com.dwusoft.ius;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.client.InfoReleaseCoreCleint;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.constant.SystemTools;
import com.apache.info.entity.PubMetadata;
import com.apache.method.ThreadPool;
import com.apache.tools.StrUtil;
import com.apache.uct.common.ToolsUtil;
import com.dwusoft.ius.able.LogWriteRunnable;
import com.dwusoft.ius.plugin.CustomMethodPlugin;
import com.dwusoft.ius.plugin.IusSqlPorxyPlugin;

/**
 * description:  
 * @author Administrator 创建时间：2016-10-18  
 */
public class DymicSqlSuper {

	protected IusSqlPorxyPlugin iusSqlPorxyPlugin;

	public void exeCustomPlugin(ParamsVo vo, String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		String keys = String.valueOf(vo.getParams("sqlKeyId"));
		if (Validator.isNotNull(keys)) {
			String beforMethodKey = String.valueOf(vo.getParams("beforMethodKey"));
			if (Validator.isNull(beforMethodKey)) {
				return;
			} else {
				vo.setMethodKey("beforMethodKey");
				vo.setParams("beforMethodKey", "");
			}
			map = (Map) getPlugin().doInvoke(vo);
			vo.setParams("sqlKeyId", "");
		} else {
			if ("befor".equals(type)) {
				String beforMethodKey = String.valueOf(vo.getParams("beforMethodKey"));
				if (Validator.isNull(beforMethodKey))
					return;
				vo.setMethodKey("beforMethodKey");
				CustomMethodPlugin befor = CustomMethodFactory.getInstance().getPluginByKey(beforMethodKey);
				if (!Validator.isEmpty(befor)) {
					map = (Map) befor.beforeInvoke(vo.getParams());
					vo.setParams("beforMethodKey", "");
				}
			}
		}
		if (!Validator.isEmpty(map)) {
			for (String key : map.keySet()) {
				vo.setParams(key, map.get(key));
			}
		}
	}

	public void exeCustomPluginForAfter(ParamsVo vo, String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		String keys = String.valueOf(vo.getParams("sqlKeyId"));
		if (Validator.isNotNull(keys)) {
			String afterMethodKey = String.valueOf(vo.getParams("afterMethodKey"));
			if (Validator.isNull(afterMethodKey)) {
				return;
			} else {
				vo.setMethodKey("afterMethodKey");
				vo.setParams("afterMethodKey", "");
			}
			map = (Map) getPlugin().doInvoke(vo);
			vo.setParams("sqlKeyId", "");
		} else {
			if ("after".equals(type)) {
				String afterMethodKey = String.valueOf(vo.getParams("afterMethodKey"));
				if (Validator.isNull(afterMethodKey))
					return;
				vo.setMethodKey("afterMethodKey");
				CustomMethodPlugin after = CustomMethodFactory.getInstance().getPluginByKey(afterMethodKey);
				if (!Validator.isEmpty(after)) {
					map = (Map) after.afterInvoke(vo.getParams());
				}
				vo.setParams("afterMethodKey", "");
			}
		}
		if (!Validator.isEmpty(map) && !map.isEmpty()) {
			for (String key : map.keySet()) {
				vo.setParams(key, map.get(key));
			}
		}
	}

	protected String dymicSql(String exeType, ParamsVo vo, String primaryKeyId, String attrStr, String modelType,
			Map<String, String> params, String tableName) {
		if (exeType.startsWith("i_")) {//插入语句
			vo.setParams("rulePrimaryKey", modelType);
			return getInsertSqlStr(params, attrStr, tableName, primaryKeyId, vo);
		} else if (exeType.startsWith("ib_")) {//插入语句
			vo.setParams("rulePrimaryKey", modelType);
			return getInsertBatchSqlStr(params, attrStr, tableName, primaryKeyId, vo);
		} else if (exeType.startsWith("u_")) {//修改语句
			vo.setParams("rulePrimaryKey", modelType);
			return getUpdateSqlStr(params, attrStr, tableName, primaryKeyId, vo);
		} else if (exeType.startsWith("d_")) {
			vo.setParams("rulePrimaryKey", modelType);
			return getDealSqlStr(params, attrStr, tableName, vo, primaryKeyId);
		} else if (exeType.startsWith("s_")) {
			return getSelectSqlStr(params, attrStr, tableName, vo, primaryKeyId);
		}
		return "";
	}

	/**
	 * description:  拼动态原生的修改语句
	 */
	public String getUpdateSqlStr(Map<String, String> params, String attrStr, String tableName, String primaryKeyId,
			ParamsVo paramVo) {
		if (Validator.isEmpty(params) || params.size() <= 0)
			return "";
		StringBuffer sb2 = new StringBuffer("");
		StringBuffer sb3 = new StringBuffer("");
		boolean mark1 = false, mark2 = false;
		int i = 0;
		String rules = String.valueOf(paramVo.getParams("exe_rules"));
		String model = String.valueOf(paramVo.getParams("rulePrimaryKey"));
		String idValue = "";
		for (String key : params.keySet()) {
			String val = params.get(key);
			if (attrStr.contains(key)) {//不带s_的情况
				//attrStr.replace(key, "");
				if (val.startsWith("[")) {
					val = getValue(rules, model, key, val);
				} else {
					rules = rules.replaceAll("\\[" + model + "." + key + "\\]", val);
				}
				String cloumn = upperCharToUnderLine(key);
				if (cloumn.equals(primaryKeyId))
					continue;
				sb2.append("," + cloumn + "='" + val + "'");
				mark1 = true;
			} else {
				String keyStr = key.substring(2);
				if (attrStr.contains(keyStr)) {
					//attrStr.replace(keyStr, "");
					if (val.startsWith("[")) {
						val = getValue(rules, model, keyStr, val);
					} else {
						rules = rules.replaceAll("\\[" + model + "." + keyStr + "\\]", val);
					}
					String cloumn = upperCharToUnderLine(keyStr);
					if (cloumn.equals(primaryKeyId)) {
						idValue = val;
					}
					if (key.startsWith("s_")) {//待修改的字段
						if ("is null".equalsIgnoreCase(val)) {
							sb2.append("," + cloumn + "=null");
						} else {
							sb2.append("," + cloumn + "='" + val + "'");
						}
						mark1 = true;
					} else if (key.startsWith("w_")) {//查询条件
						if (i == 0) {
							sb3.append(cloumn + "='" + val + "'");
							i++;
						} else {
							sb3.append(" and " + cloumn + "='" + val + "'");
						}
						mark2 = true;
					}
				}
			}
		}
		if (!mark1 || !mark2)
			return "";
		sb2.deleteCharAt(0);
		paramVo.setParams("exe_rules", rules);
		if (Validator.isNotNull(idValue)) {
			setObjIds(paramVo, idValue, primaryKeyId);
			params.put(toCamelNamed(primaryKeyId), idValue);
		}
		saveLog(params, paramVo, "edit", tableName);//记录日志
		return "UPDATE " + tableName + " SET " + sb2.toString() + " where " + sb3.toString();
	}

	/**
	 * description:  拼动态原生的插入语句
	 */
	public String getInsertSqlStr(Map<String, String> params, String attrStr, String tableName, String primaryKeyId,
			ParamsVo paramVo) {
		if (Validator.isEmpty(params) || params.size() <= 0)
			return "";
		StringBuffer colName = new StringBuffer("");
		StringBuffer colValue = new StringBuffer("");
		String rules = String.valueOf(paramVo.getParams("exe_rules"));
		String model = String.valueOf(paramVo.getParams("rulePrimaryKey"));
		if ("uct_user".equalsIgnoreCase(tableName)) {
			params.put("createTime", System.currentTimeMillis() + "");
			params.put("updateTime", System.currentTimeMillis() + "");
		}
		boolean mark1 = false;
		String ifpkId = "";//是否有主键
		for (String key : params.keySet()) {
			String val = params.get(key);
			if (attrStr.contains(key)) {//不带s_的情况
				mark1 = true;
				String cloumn = upperCharToUnderLine(key);
				if (cloumn.equals(primaryKeyId) && Validator.isNull(val))
					continue;
				if (val.startsWith("[")) {
					val = getValue(rules, model, key, val);
					colValue.append(",'" + val + "'");
				} else {
					colValue.append(",'" + val + "'");
					rules = rules.replaceAll("\\[" + model + "." + key + "\\]", val);
				}
				if (cloumn.equals(primaryKeyId)) {
					ifpkId = val;
				}
				colName.append("," + cloumn);
			}
		}

		if (!mark1)
			return "";
		colName.deleteCharAt(0);
		colValue.deleteCharAt(0);

		String id = Validator.generate();
		if (Validator.isNotNull(params.get(toCamelNamed(primaryKeyId))) || Validator.isNotNull(ifpkId)) {
			id = StrUtil.doNull(ifpkId, params.get(toCamelNamed(primaryKeyId)));
		}
		if (Validator.isNull(ifpkId)) {
			colName.append("," + primaryKeyId);
			colValue.append(",'" + id + "'");
		}
		rules = rules.replaceAll("\\[" + model + "." + toCamelNamed(primaryKeyId) + "\\]", id);
		paramVo.setParams("exe_rules", rules);

		setObjIds(paramVo, id, primaryKeyId);
		params.put(toCamelNamed(primaryKeyId), id);
		saveLog(params, paramVo, "save", tableName);//记录日志
		return "insert into " + tableName + " (" + colName.toString() + " ) values ( " + colValue.toString() + ")";
	}

	/**
	 * description:  拼动态原生的插入语句（批量）
	 */
	public String getInsertBatchSqlStr(Map<String, String> params, String attrStr, String tableName,
			String primaryKeyId, ParamsVo paramVo) {
		if (Validator.isEmpty(params) || params.size() <= 0)
			return "";
		String rules = String.valueOf(paramVo.getParams("exe_rules"));
		String model = String.valueOf(paramVo.getParams("rulePrimaryKey"));
		if ("uct_user".equalsIgnoreCase(tableName)) {
			params.put("createTime", System.currentTimeMillis() + "");
			params.put("updateTime", System.currentTimeMillis() + "");
		}
		boolean mark1 = false;
		boolean ifpkId = false;

		String splitAttr = params.get("splitAttr");///用来表示分隔数据列;'-#-'分隔符
		String splitValues[] = params.get(splitAttr).split("-#-");
		int count = splitValues.length;
		StringBuffer ids = new StringBuffer();
		StringBuffer inserts = new StringBuffer("");
		for (int i = 0; i < count; i++) {
			StringBuffer colName = new StringBuffer("");
			StringBuffer colValue = new StringBuffer("");
			for (String key : params.keySet()) {
				String val = "";
				if (key.equalsIgnoreCase(splitAttr)) {
					val = splitValues[i];
				} else {
					val = params.get(key);
					if (val.indexOf("-#-") != -1) {
						String splitVals[] = val.split("-#-");
						if ((splitVals.length - 1) < i) {
							val = "";
						} else {
							val = splitVals[i];
						}
					}
				}
				if (attrStr.indexOf(key) != -1) {//符合徒插入字段
					mark1 = true;
					String cloumn = upperCharToUnderLine(key);//将类属性改成数据字段名
					if (val.startsWith("[")) {
						val = getValue(rules, model, key, val);//匹配值
					}
					if (cloumn.equals(primaryKeyId)) {//是否主键列
						ifpkId = true;
						ids.append("," + val);
					}
					colValue.append(",'" + val + "'");
					colName.append("," + cloumn);
				}
			}
			if (!ifpkId) {//不含主键，给主键生成值
				String id = Validator.generate();
				colValue.append(",'" + id + "'");
				colName.append("," + primaryKeyId);
				ids.append("," + id);
				ifpkId = false;
			}
			colName.deleteCharAt(0);
			colValue.deleteCharAt(0);
			inserts.append(";insert into " + tableName + " (" + colName.toString() + " ) values ( "
					+ colValue.toString() + ")");
		}

		if (!mark1)
			return "";
		inserts.deleteCharAt(0);
		ids.deleteCharAt(0);

		setObjIds(paramVo, ids.toString(), primaryKeyId);
		//saveLog(params, paramVo, "save", tableName);//记录日志
		return inserts.toString();
	}

	/**
	 * description:  拼动态原生的删除语句
	 */
	public String getDealSqlStr(Map<String, String> params, String attrStr, String tableName, ParamsVo paramVo,
			String primaryKeyId) {
		if (Validator.isEmpty(params) || params.size() <= 0)
			return "";
		StringBuffer sb2 = new StringBuffer("");
		boolean mark1 = false;
		int i = 0;
		String idValue = "";
		String rules = String.valueOf(paramVo.getParams("exe_rules"));//规则
		String model = String.valueOf(paramVo.getParams("rulePrimaryKey"));//模型名,如：trpPub
		for (String key : params.keySet()) {
			String keyStr = key.substring(2);
			String val = params.get(key);
			if (attrStr.indexOf(keyStr) != -1) {
				String cloumn = upperCharToUnderLine(keyStr);
				if (val.startsWith("[")) {
					val = getValue(rules, model, keyStr, val);//从规则中获取值
				} else {
					rules = rules.replaceAll("\\[" + model + "." + keyStr + "\\]", val);//替换规则中的值
				}
				if (cloumn.equals(primaryKeyId)) {
					idValue = params.get(key);
				}
				if (key.startsWith("w_")) {//查询条件
					if (i == 0) {
						sb2.append(cloumn + "='" + val + "'");
						i++;
					} else {
						sb2.append(" and " + cloumn + "='" + val + "'");
					}
					mark1 = true;
				} else {
					if (i == 0) {
						sb2.append(cloumn + "='" + val + "'");
						i++;
					} else {
						sb2.append(" and " + cloumn + "='" + val + "'");
					}
					mark1 = true;
				}
			}
		}
		if (!mark1)
			return "";
		//sb2.deleteCharAt(0);
		if (Validator.isNotNull(idValue)) {
			params.put(toCamelNamed(primaryKeyId), idValue);
		}
		paramVo.setParams("exe_rules", rules);
		saveLog(params, paramVo, "delete", tableName);//记录日志
		return "delete from " + tableName + " where " + sb2.toString();
	}

	/**
	 * description:  拼动态原生的修改语句
	 */
	public String getSelectSqlStr(Map<String, String> params, String attrStr, String tableName, ParamsVo paramVo,
			String primaryKeyId) {
		String orderBy = Validator.getDefaultStr(String.valueOf(paramVo.getParams("orderBy")), "");
		if (Validator.isNotNull(orderBy))
			orderBy = " order by " + orderBy;
		String rules = String.valueOf(paramVo.getParams("exe_rules"));
		if (Validator.isNull(rules)) {
			String sql = "select * from " + tableName + " where 1=1 ";
			if (Validator.isEmpty(params) || params.size() <= 0) {
			} else {
				for (String key : params.keySet()) {
					String keyVal = key.substring(2);
					if (key.startsWith("wl_") || key.startsWith("wi_") || key.startsWith("wb_")) {
						keyVal = key.substring(3);
					}
					if (attrStr.indexOf(keyVal) != -1) {
						String cloumn = upperCharToUnderLine(keyVal);
						String value = params.get(key);
						String content = "='" + value + "'";
						if (key.startsWith("wl_")) {//查询条件
							content = " like '%" + value + "%'";
						} else if (key.startsWith("wi_")) {
							content = " in(" + StrUtil.strArrayToStr(value.split(","), ",", "'") + ")";
						} else if (key.startsWith("wb_") && Validator.isNotNull(value)) {
							String whs[] = value.split(",");
							if (whs.length > 1) {
								if (whs[0].equals("-")) {//不比较开始值用'-'表示
									sql += " and " + cloumn + "<='" + whs[1] + "'";
								} else {
									sql += " and '" + whs[0] + "'<=" + cloumn + " and " + cloumn + "<='" + whs[1] + "'";
								}
							} else {
								sql += " and " + cloumn + ">='" + whs[0] + "'";
							}
							content = "";
						}
						if (Validator.isNotNull(content))
							sql += " and " + cloumn + content;
					}
				}
			}
			return sql + " " + orderBy;
		}

		StringBuffer sb3 = new StringBuffer("");
		boolean mark2 = false;
		int i = 0;
		String model = String.valueOf(paramVo.getParams("rulePrimaryKey"));
		for (String key : params.keySet()) {
			String val = params.get(key);
			String keyStr = key.substring(2);
			if (key.startsWith("wl_") || key.startsWith("wi_") || key.startsWith("wb_")) {
				keyStr = key.substring(3);
			}
			if (val.startsWith("[")) {
				val = getValue(rules, model, keyStr, val);
			}
			if (attrStr.indexOf(keyStr) != -1) {
				String cloumn = upperCharToUnderLine(keyStr);
				String content = "='" + val + "'";
				if (key.startsWith("wl_")) {//查询条件
					content = " like '%" + val + "%'";
				} else if (key.startsWith("wi_")) {
					content = " in(" + StrUtil.strArrayToStr(val.split(","), ",", "'") + ")";
				} else if (key.startsWith("wb_") && Validator.isNotNull(val)) {
					String whs[] = val.split(",");
					if (whs.length > 1) {
						if (whs[0].equals("-")) {//不比较开始值用'-'表示
							sb3.append(" and " + cloumn + "<='" + whs[1] + "'");
						} else {
							sb3.append(" and '" + whs[0] + "'<=" + cloumn + " and " + cloumn + "<='" + whs[1] + "'");
						}
					} else {
						sb3.append(" and " + cloumn + ">='" + whs[0] + "'");
					}
					content = "";
				}
				if (Validator.isNotNull(content))
					sb3.append(" and " + cloumn + content);
				mark2 = true;
			}
		}
		if (!mark2) {
			return "";
		}
		return "select * from " + tableName + " where 1=1 " + sb3.toString() + " " + orderBy;
	}

	/**
	 * description:  拼动态原生的修改语句
	 */
	public String getSelectMoreSqlStr(ParamsVo vo, String models[], String sign) {
		String colmuns = String.valueOf(vo.getParams("result_column"));
		StringBuffer sbc = new StringBuffer("");
		StringBuffer sbw = new StringBuffer("");
		StringBuffer sbt = new StringBuffer("");
		StringBuffer sbo = new StringBuffer("");//排序

		boolean mark = Validator.isNotNull(colmuns) ? true : false;
		if (mark) {
			String model[] = colmuns.split(",");
			for (int q = 0; q < model.length; q++) {
				String objName = model[q].substring(0, model[q].indexOf("."));
				String clomun = upperCharToUnderLine(model[q].substring(model[q].indexOf(".")));
				if (Validator.isNotNull(sign)) {
					sbc.append("," + objName + clomun + " as \"" + objName + sign + clomun.substring(1) + "\"");
				} else {
					sbc.append("," + objName + clomun);
				}
			}
		}
		for (int i = 0; i < models.length; i++) {
			String prefix = models[i].substring(2);
			String attr = ToolsUtil.getInstance().getValueByKey("custom-method.properties", "obj_" + prefix);
			if (Validator.isNull(attr))
				attr = densgerSql(vo, prefix);
			if (Validator.isNull(attr))
				continue;
			String attrStr[] = attr.split(";");
			sbt.append("," + attrStr[3] + " " + prefix);

			String model[] = attrStr[1].split(",");
			for (int q = 0; q < model.length; q++) {
				if (!mark) {
					String clomun = upperCharToUnderLine(model[q]);
					if (Validator.isNotNull(sign)) {
						sbc.append("," + prefix + "." + clomun + " as \"" + prefix + sign + clomun + "\"");
					} else {
						sbc.append("," + prefix + "." + clomun);
					}
				}
				String whereStr = String.valueOf(vo.getParams(prefix + ".w_" + model[q]));//查询条件
				if (Validator.isNotNull(whereStr)) {
					//whereStr = String.valueOf(vo.getParams(prefix + ".w_" + model[q]));//查询条件
					//if (Validator.isNotNull(whereStr)) {
					sbw.append(" and " + prefix + "." + upperCharToUnderLine(model[q]) + "='" + whereStr + "'");
					//}
				} else {
					whereStr = String.valueOf(vo.getParams(prefix + ".wl_" + model[q]));//查询条件
					if (Validator.isNotNull(whereStr)) {
						//	whereStr = String.valueOf(vo.getParams(prefix + ".wl_" + model[q]));//查询条件(like)
						//if (Validator.isNotNull(whereStr)) {
						sbw.append(" and " + prefix + "." + upperCharToUnderLine(model[q]) + " like '%" + whereStr
								+ "%'");
						//}
					} else {
						whereStr = String.valueOf(vo.getParams(prefix + ".wi_" + model[q]));//查询条件(in)
						if (Validator.isNotNull(whereStr)) {
							sbw.append(" and " + prefix + "." + upperCharToUnderLine(model[q]) + " in("
									+ StrUtil.strArrayToStr(whereStr.split(","), ",", "'") + ")");
						} else {
							whereStr = String.valueOf(vo.getParams(prefix + ".wb_" + model[q]));//查询条件(<=  >=)开始值不能为空
							if (Validator.isNotNull(whereStr)) {
								String whs[] = whereStr.split(",");
								String colmun = prefix + "." + upperCharToUnderLine(model[q]);
								if (whs.length > 1) {
									if (whs[0].equals("-")) {//不比较开始值用'-'表示
										sbw.append(" and " + colmun + "<='" + whs[1] + "'");
									} else {
										sbw.append(" and '" + whs[0] + "'<=" + colmun + " and " + colmun + "<='"
												+ whs[1] + "'");
									}
								} else {
									sbw.append(" and " + colmun + ">='" + whs[0] + "'");
								}
							}
						}
					}
				}
				String wherekey = String.valueOf(vo.getParams(prefix + "." + model[q]));//查询条件
				if (Validator.isNotNull(wherekey)) {
					if (wherekey.startsWith("[")) {
						wherekey = wherekey.replace("[", "");
						wherekey = wherekey.replace("]", "");
						sbw.append(" and " + prefix + "." + upperCharToUnderLine(model[q]) + "="
								+ wherekey.substring(0, wherekey.indexOf("."))
								+ upperCharToUnderLine(wherekey.substring(wherekey.indexOf("."))) + "");
					} else {
						sbw.append(" and " + prefix + "." + upperCharToUnderLine(model[q]) + "='" + wherekey + "'");
					}
				}
			}

		}
		sbc.deleteCharAt(0);
		sbt.deleteCharAt(0);
		String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")), "");
		if (Validator.isNotNull(orderBy))
			orderBy = "order by " + orderBy;
		return "select " + sbc.toString() + " from " + sbt.toString() + " where 1=1 " + sbw.toString() + " " + orderBy;
	}

	private String getSqlContent(String prefix, String columnStr, String value) {
		String keyStr = columnStr.substring(2);
		if (columnStr.startsWith("wl_")) {
			keyStr = columnStr.substring(3);
		}
		String cloumn = upperCharToUnderLine(keyStr);
		String content = "='" + value + "'";
		if (columnStr.startsWith("wl_")) {//查询条件
			content = " like '%" + value + "%'";
		}
		return cloumn + content;
	}

	public String densgerSql(ParamsVo vo, String prefix) {
		String sortCode = String.valueOf(vo.getParams("sortCode"));
		String tab = "";
		if (Validator.isNull(sortCode)) {
			tab = upperCharToUnderLine(prefix);
			if (tab.indexOf("_") == -1)
				return "";
			sortCode = tab.substring(tab.indexOf("_") + 1);
		} else {
			tab = "pub_" + sortCode;
		}
		String key = sortCode + "_tableAttr";
		String content = String.valueOf(SystemTools.getInstance().getCache("").getCacheObjectByKey(key));
		if (Validator.isNotNull(content))
			return content;
		List<PubMetadata> list = InfoReleaseCoreCleint.getInstance().getMetadatas(sortCode);
		if (null == list)
			return "";
		StringBuffer sb = new StringBuffer("sortCode;");
		String primaryKey = "";
		for (PubMetadata pm : list) {
			if ("1".equals(pm.getDataRestrict())) {//处理主键
				primaryKey = pm.getDataShortName();
			}
			sb.append(pm.getDataAttr() + ",");
		}
		sb.append(";" + primaryKey + ";" + tab);
		return sb.toString();
	}

	/**
	 * description:  组装对象参数
	 */
	private void setObjIds(ParamsVo vo, String objId, String primarkId) {
		String modelType = String.valueOf(vo.getParams("rulePrimaryKey"));
		Map<String, String> ids = (Map) vo.getParams("resultIds");
		if (Validator.isEmpty(ids)) {
			ids = new HashMap<String, String>();
		}
		ids.put(modelType + "_" + toCamelNamed(primarkId), objId);
		vo.setParams("resultIds", ids);
	}

	private String getValue(String rules, String model, String attr, String val) {
		String attrName = model + "." + attr;
		if (val.startsWith("[")) {
			int ct = rules.indexOf(attrName + ":");
			if (ct > -1) {
				String str[] = rules.substring(ct).split(",");
				for (int i = 0; i < str.length; i++) {
					if (str[i].startsWith(attrName)) {
						return str[i].substring(attrName.length() + 1);
					}
				}
			}
		}
		return val;
	}

	/**
	 * description:  获取类中所有属性值
	 */
	protected String getReflectAttr(Object model) {
		StringBuffer sb = new StringBuffer("");
		Field[] field = model.getClass().getDeclaredFields(); //获取实体类的所有属性，返回Field数组  
		for (int j = 0; j < field.length; j++) { //遍历所有属性
			sb.append(field[j].getName() + ","); //获取属性的名字
		}
		return sb.toString();
	}

	/**
	 * description:  将类属性改成数据字段名
	 */
	protected String upperCharToUnderLine(String param) {
		Pattern p = Pattern.compile("[A-Z]");
		if (param == null || param.equals("")) {
			return "";
		}
		StringBuilder builder = new StringBuilder(param);
		Matcher mc = p.matcher(param);
		int i = 0;
		while (mc.find()) {
			builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
			i++;
		}
		if ('_' == builder.charAt(0)) {
			builder.deleteCharAt(0);
		}
		return builder.toString();
	}

	/**
	 * 添加日志信息
	 */
	@SuppressWarnings("unchecked")
	private void saveLog(Map logMap, ParamsVo paramVo, String logType, String className) {
		logMap.put("log_returnValue", "true");
		logMap.put("logType", logType);
		logMap.put("logClassName", toCamelNamed(className));
		logMap.put("logIp", String.valueOf(paramVo.getParams("logReqIp")));
		logMap.put("log_userEname", String.valueOf(paramVo.getParams("logOperatorUser")));
		LogWriteRunnable runable = new LogWriteRunnable(logMap);
		ThreadPool.newFixedThreadPool(1).execute(runable);
	}

	/**
	 * 属性值生成,符合骆驼命名
	 */
	protected String toCamelNamed(String str) {
		String names[] = str.split("_");
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < names.length; i++) {
			if (i == 0) {
				sb.append(names[i].toLowerCase());
			} else {
				sb.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1).toLowerCase());
			}
		}
		return sb.toString();
	}

	protected IusSqlPorxyPlugin getPlugin() {
		if (null == iusSqlPorxyPlugin) {
			iusSqlPorxyPlugin = (IusSqlPorxyPlugin) SpringContextLoader.getBean("iusSqlPorxyPlugin");
		}
		return iusSqlPorxyPlugin;
	}
}
